package com.xju.admin.security;

import com.xju.admin.entity.User;
import com.xju.admin.service.UserService;
import com.xju.admin.utils.SecurityUserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Component
public class AdminSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        UserDetails userDetail= (UserDetails) authentication.getPrincipal();
        User user=userService.findByName(userDetail.getUsername());
        user.setUserLoginTime(new Date());
        userService.updateUser(user);
        super.onAuthenticationSuccess(request, response, authentication);
    }
}
